from tkinter import Tk
from tkinter import Label
from tkinter import Entry
from tkinter import Frame
from tkinter import Button


class WindowLogin(Tk):
    """登录窗口"""

    def __init__(self):
        """初始化登录窗口"""
        super(WindowLogin, self).__init__()
        # 设置窗口属性
        self.window_init()
        # 填充控件
        self.add_widgets()

        # self.on_reset_button_clicked(lambda: self.))

    def window_init(self):
        """初始化窗口属性"""
        # 设置窗口标题
        self.title('登录窗口')
        # 设置窗口不能被拉伸
        self.resizable(width=False, height=False)
        # 设置窗口大小和位置
        width = 255
        height = 95
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = (screen_width - width) // 2
        y = (screen_height - height) // 2
        self.geometry(f"{width}x{height}+{x}+{y}")


    def add_widgets(self):
        """添加空间到窗口里面"""
        # 用户名
        username_label = Label(self)
        username_label['text'] = '用户名：'
        username_label.grid(column=0, row=0,padx=5,pady=5)

        username_entry = Entry(self,name='username_entry')
        username_entry.grid(column=1, row=0)
        username_entry['width'] = 25
        #密码
        password_label = Label(self)
        password_label['text'] = '密 码：'
        password_label.grid(column=0, row=1, padx=5, pady=5)

        password_entry = Entry(self, name='password_entry')
        password_entry.grid(column=1, row=1)
        password_entry['show'] = '*'
        password_entry['width'] = 25
        #按钮
        button_frame = Frame(self,name='button_frame')

        # 重置按钮
        reset_button =  Button(button_frame,name='reset_button')
        reset_button['text'] = ' 重置'
        reset_button.pack(side='left',padx=20)

        # 登录按钮
        login_button = Button(button_frame, name='login_button')
        login_button['text'] = ' 登录'
        login_button.pack(side='left')

        button_frame.grid(columnspan=2, row=2,pady=5)

    def get_username(self):
        """获取用户名"""
        return self.children['username_entry'].get()

    def get_password(self):
        """获取用户名"""
        return self.children['password_entry'].get()

    def clear_username(self):
        """用户名"""
        self.children['username_entry'].delete(0,'end')

    def clear_password(self):
        """清空密码"""
        self.children['password_entry'].delete(0, 'end')

    def on_reset_button_clicked(self,command):
        """登录按钮事件"""
        reset_btn = self.children['button_frame'].children['reset_button']
        reset_btn['command'] = command  # 把command函数赋值给登录按钮的command 当点击时会调用

    def on_login_button_clicked(self,command):
        """按钮事件"""
        login_btn = self.children['button_frame'].children['login_button']
        login_btn['command'] = command # 把command函数赋值给登录按钮的command 当点击时会调用

    def on_window_close_clicked(self,command):
        """关闭窗口事件的处理方法"""
        self.protocol('WM_DELETE_WINDOW',command)



if __name__ == '__main__':
    app = WindowLogin()
    app.mainloop()